package net.gyapp.markim.module.message;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.easyder.wrapper.base.view.WrapperStatusActivity;
import com.gyf.barlibrary.ImmersionBar;
import com.lqr.emoji.EmotionKeyboard;
import com.lqr.emoji.EmotionLayout;
import com.lqr.emoji.IEmotionExtClickListener;
import com.lqr.emoji.IEmotionSelectedListener;
import com.luck.picture.lib.PictureSelectionModel;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;

import net.gyapp.markim.R;
import net.gyapp.markim.module.basic.base.CommonPresenter;
import net.gyapp.markim.module.expand.recorder.EaseVoiceRecorderView;
import net.gyapp.markim.utils.SimpleTextWatcher;
import net.gyapp.markim.widget.IndexRecyclerView;

import butterknife.BindView;
import me.winds.widget.usage.TitleView;

/**
 * Author:  winds
 * Data:    2018/7/18
 * Version: 1.0
 * Desc:
 */


public abstract class WrapperConversationActivity<P extends CommonPresenter> extends WrapperStatusActivity<P> implements IEmotionSelectedListener {

    @BindView(R.id.mRecyclerView)
    IndexRecyclerView mRecyclerView;
    @BindView(R.id.iv_audio)
    ImageView ivAudio;
    @BindView(R.id.et_content)
    EditText etContent;
    @BindView(R.id.btn_audio)
    Button btnAudio;
    @BindView(R.id.iv_emogi)
    ImageView ivEmogi;
    @BindView(R.id.iv_more)
    ImageView ivMore;
    @BindView(R.id.btn_send)
    Button btnSend;
    @BindView(R.id.layout_content)
    LinearLayout layoutContent;
    @BindView(R.id.el_emotion)
    EmotionLayout elEmotion;
    @BindView(R.id.ivAlbum)
    ImageView ivAlbum;
    @BindView(R.id.ivShot)
    ImageView ivShot;
    @BindView(R.id.ivLocation)
    ImageView ivLocation;
    @BindView(R.id.ivRedPack)
    ImageView ivRedPack;
    @BindView(R.id.tv_status)
    TextView tvStatus;
    @BindView(R.id.layout_emotion)
    FrameLayout layoutEmotion;
    @BindView(R.id.layout_more)
    LinearLayout layoutMore;
    @BindView(R.id.layout_group_project)
    FrameLayout layoutGroupProject;
    @BindView(R.id.tv_project_name)
    TextView tvProjectName;
    @BindView(R.id.voice_recorder)
    EaseVoiceRecorderView voiceRecorderView;

    protected EmotionKeyboard mEmotionKeyboard;

    @Override
    protected int getViewLayout() {
        return R.layout.activity_chat;
    }

    @Override
    protected void setImmersionBar() {
        super.setImmersionBar();
        ImmersionBar.with(this).keyboardEnable(true).init();
    }

    @Override
    protected void initView(Bundle savedInstanceState, TitleView titleView, Intent intent) {
        super.initView(savedInstanceState, titleView, intent);
        setInterceptable(false);
        setAutoFocus(false);
        initEmotionKeyboard();
        initListener();
    }


    @Override
    public void onBackPressedSupport() {
        if (elEmotion.isShown() || layoutMore.isShown()) {
            mEmotionKeyboard.interceptBackPress();
        } else {
            super.onBackPressedSupport();
        }
    }


    @Override
    protected void onDestroy() {
        hideKeyboard(getCurrentFocus());
        super.onDestroy();
    }

    public void showPictureSelector() {
        showPictureSelector(1, true, true, false, false);
    }

    /**
     * 选取图片
     *
     * @param maxSelectNum 选取图片的数量  多选时才生效
     * @param singleMode   是否单选
     * @param showCamera   是否显示拍照按钮
     * @param crop         是否剪裁
     * @param isCircleMode 剪裁是否显示圆形参考   用于裁剪圆形头像    开启裁剪时生效
     */
    public void showPictureSelector(int maxSelectNum, boolean singleMode, boolean showCamera, boolean crop, boolean isCircleMode) {
        PictureSelectionModel model = PictureSelector.create(mActivity)
                .openGallery(PictureMimeType.ofImage()) //查看类型  仅图片,可选择其它或全部
                .theme(com.easyder.wrapper.R.style.picture_default_style)
                .maxSelectNum(maxSelectNum) //最大图片选择数量
                .imageSpanCount(4)// 每行显示个数
                .minSelectNum(1)// 最小选择数量
                .selectionMode(singleMode ?
                        PictureConfig.SINGLE : PictureConfig.MULTIPLE)// 单选 or 多选
                .isCamera(showCamera);// 是否显示拍照按钮


        if (crop) {
            model
                    .enableCrop(crop)             //开启裁剪
                    .hideBottomControls(false)  //是否隐藏裁剪工具栏
                    .circleDimmedLayer(isCircleMode)    //显示圆形参考图层
                    .withAspectRatio(1, 1);     //裁剪比例
        }
        model.forResult(PictureConfig.CHOOSE_REQUEST);//结果回调onActivityResult code
    }

    /***********************************************************************************************/
    @Override
    public void onEmojiSelected(String key) {

    }

    @Override
    public void onStickerSelected(String categoryName, String stickerName, String stickerBitmapPath) {

    }


    public void initListener() {
//        mRecyclerView.setOnTouchListener(new View.OnTouchListener() {
//            @Override
//            public boolean onTouch(View v, MotionEvent event) {
//                hideKeyboard(getCurrentFocus());
//                if (elEmotion.isShown() || layoutMore.isShown()) {
//                    mEmotionKeyboard.interceptBackPress();
//                }
//                return false;
//            }
//        });
        elEmotion.setEmotionSelectedListener(this);
        elEmotion.setEmotionAddVisiable(false);
        elEmotion.setEmotionSettingVisiable(false);
        elEmotion.setEmotionExtClickListener(new IEmotionExtClickListener() {
            @Override
            public void onEmotionAddClick(View view) {
                Toast.makeText(getApplicationContext(), "add", Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onEmotionSettingClick(View view) {
                Toast.makeText(getApplicationContext(), "setting", Toast.LENGTH_SHORT).show();
            }
        });
        layoutContent.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        closeBottomAndKeyboard();
                        break;
                }
                return false;
            }
        });
        ivAudio.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (btnAudio.isShown()) {
                    hideAudioButton();
                    etContent.requestFocus();
                    if (mEmotionKeyboard != null) {
                        mEmotionKeyboard.showSoftInput();
                    }
                } else {
                    showAudioButton();
                    hideEmotionLayout();
                    hideMoreLayout();
                }
            }
        });
        etContent.addTextChangedListener(new SimpleTextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (etContent.getText().toString().trim().length() > 0) {
                    btnSend.setVisibility(View.VISIBLE);
                    ivMore.setVisibility(View.GONE);
                } else {
                    btnSend.setVisibility(View.GONE);
                    ivMore.setVisibility(View.VISIBLE);
                }
            }


        });

        btnAudio.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(final View v, MotionEvent event) {
                voiceRecorderView.onPressToSpeakBtnTouch(v, event, new EaseVoiceRecorderView.EaseVoiceRecorderCallback() {
                    @Override
                    public void onVoiceRecordComplete(String voiceFilePath, int voiceTimeLength) {
                        sendVoiceMessage(voiceFilePath, voiceTimeLength);
                    }
                });
                return true;
            }
        });

        btnSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String content = etContent.getText().toString();
                if (!TextUtils.isEmpty(content)) {
                    sendTextMessage(content);
                    etContent.setText(null);
                }

            }
        });
    }


    private void initEmotionKeyboard() {
        elEmotion.attachEditText(etContent);
        elEmotion.setEmotionAddVisiable(false);
        elEmotion.setEmotionSettingVisiable(false);
        mEmotionKeyboard = EmotionKeyboard.with(this);
        mEmotionKeyboard.bindToEditText(etContent);
        mEmotionKeyboard.bindToContent(layoutContent);
        mEmotionKeyboard.setEmotionLayout(layoutEmotion);
        mEmotionKeyboard.bindToEmotionButton(ivEmogi, ivMore);
        mEmotionKeyboard.setOnEmotionButtonOnClickListener(new EmotionKeyboard.OnEmotionButtonOnClickListener() {
            @Override
            public boolean onEmotionButtonOnClickListener(View view) {
                switch (view.getId()) {
                    case R.id.iv_emogi:
                        if (!elEmotion.isShown()) {
                            if (layoutMore.isShown()) {
                                showEmotionLayout();
                                hideMoreLayout();
                                hideAudioButton();
                                return true;
                            }
                        } else if (elEmotion.isShown() && !layoutMore.isShown()) {
                            ivEmogi.setImageResource(R.drawable.ic_cheat_emo);
                            return false;
                        }
                        showEmotionLayout();
                        hideMoreLayout();
                        hideAudioButton();
                        break;
                    case R.id.iv_more:
                        if (!layoutMore.isShown()) {
                            if (elEmotion.isShown()) {
                                showMoreLayout();
                                hideEmotionLayout();
                                hideAudioButton();
                                return true;
                            }
                        }
                        showMoreLayout();
                        hideEmotionLayout();
                        hideAudioButton();
                        break;
                }
                return false;
            }
        });
    }


    private void showAudioButton() {
        btnAudio.setVisibility(View.VISIBLE);
        etContent.setVisibility(View.GONE);
        ivAudio.setImageResource(R.drawable.ic_cheat_keyboard);

        if (layoutEmotion.isShown()) {
            if (mEmotionKeyboard != null) {
                mEmotionKeyboard.interceptBackPress();
            }
        } else {
            if (mEmotionKeyboard != null) {
                mEmotionKeyboard.hideSoftInput();
            }
        }
    }

    private void hideAudioButton() {
        btnAudio.setVisibility(View.GONE);
        etContent.setVisibility(View.VISIBLE);
        ivAudio.setImageResource(R.drawable.ic_cheat_voice);
    }

    private void showEmotionLayout() {
        elEmotion.setVisibility(View.VISIBLE);
        ivEmogi.setImageResource(R.drawable.ic_cheat_keyboard);
    }

    private void hideEmotionLayout() {
        elEmotion.setVisibility(View.GONE);
        ivEmogi.setImageResource(R.drawable.ic_cheat_emo);
    }

    private void showMoreLayout() {
        layoutMore.setVisibility(View.VISIBLE);
    }

    private void hideMoreLayout() {
        layoutMore.setVisibility(View.GONE);
    }

    private void closeBottomAndKeyboard() {
        elEmotion.setVisibility(View.GONE);
        layoutMore.setVisibility(View.GONE);
        if (mEmotionKeyboard != null) {
            mEmotionKeyboard.interceptBackPress();
        }
    }

    /***********************************************************************************************/

    protected abstract void sendTextMessage(String content);

    protected abstract void sendVoiceMessage(String voiceFilePath, int voiceTimeLength);
}
